package com.cskaoyan.service.wxhome;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.model.vo.wxhomeindex.WxHomeIndexFloorGoodsListBO;
import com.cskaoyan.model.vo.wxhomeindex.WxHomeIndexVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: project-market
 * @description:
 * @author: Jesson Guo
 * @create: 2021-12-03 17:40
 **/
@Service
public class WxHomeIndexServiceImpl implements WxHomeIndexService {
    @Autowired
    MallGoodsMapper goodsMapper;
    @Autowired
    MallSystemMapper systemMapper;
    @Autowired
    MallCategoryMapper categoryMapper;
    @Autowired
    MallCouponUserMapper couponUserMapper;
    @Autowired
    MallCouponMapper couponMapper;
    @Autowired
    MallAdMapper adMapper;
    @Autowired
    MallBrandMapper brandMapper;
    @Autowired
    MallTopicMapper topicMapper;

    @Override
    public WxHomeIndexVO getHomeIndex(Integer id) {
        //响应data共七部分
        WxHomeIndexVO wxHomeIndexVO = new WxHomeIndexVO();
        //part1 newGoodsList
        //获取分页
        int newGoodsLimit = Integer.parseInt(systemMapper.selectByPrimaryKey(2).getKeyValue());
        PageHelper.startPage(1, newGoodsLimit);
        //查询信息
        MallGoodsExample newGoodsListExample = new MallGoodsExample();
        MallGoodsExample.Criteria newGoodsListExampleCriteria = newGoodsListExample.createCriteria();
        //不删 是new
        newGoodsListExampleCriteria.andDeletedEqualTo(false);
        newGoodsListExampleCriteria.andIsNewEqualTo(true);
//        newGoodsListExample.setOrderByClause("sort_order" + " " + "desc");
        List<MallGoods> newGoodsList = goodsMapper.selectByExample(newGoodsListExample);
        wxHomeIndexVO.setNewGoodsList(newGoodsList);


        // 显示所有优惠券
        MallCouponExample noUserIdCouponExample = new MallCouponExample();
        MallCouponExample.Criteria noUserIdCouponExampleCriteria = noUserIdCouponExample.createCriteria();
        noUserIdCouponExampleCriteria.andDeletedEqualTo(false);
        List<MallCoupon> noUserIdCouponList = couponMapper.selectByExample(noUserIdCouponExample);
        wxHomeIndexVO.setCouponList(noUserIdCouponList);



        //part3 channel
        int categoryLimit = Integer.parseInt(systemMapper.selectByPrimaryKey(15).getKeyValue());
        PageHelper.startPage(1, categoryLimit);
        MallCategoryExample homeIndexCategoryExample = new MallCategoryExample();
        MallCategoryExample.Criteria categoryExampleCriteria = homeIndexCategoryExample.createCriteria();
        categoryExampleCriteria.andDeletedEqualTo(false);
        categoryExampleCriteria.andPidEqualTo(0);
//        homeIndexCategoryExample.setOrderByClause("sort_order" + " " + "desc");
        List<MallCategory> categoryList = categoryMapper.selectByExample(homeIndexCategoryExample);
        wxHomeIndexVO.setChannel(categoryList);

        //part4 group on list

        //part5 banner-->mallad
        MallAdExample adExample = new MallAdExample();
        MallAdExample.Criteria adExampleCriteria = adExample.createCriteria();
        adExampleCriteria.andDeletedEqualTo(false);
        List<MallAd> adList = adMapper.selectByExample(adExample);
        wxHomeIndexVO.setBanner(adList);

        //part6 brandList
        int brandListLimit = Integer.parseInt(systemMapper.selectByPrimaryKey(17).getKeyValue());
        PageHelper.startPage(1, brandListLimit);
        MallBrandExample brandExample = new MallBrandExample();
        MallBrandExample.Criteria brandExampleCriteria = brandExample.createCriteria();
        brandExampleCriteria.andDeletedEqualTo(false);
        List<MallBrand> brandList = brandMapper.selectByExample(brandExample);
        wxHomeIndexVO.setBrandList(brandList);

        //part7 hotGoodsList
        int hotListLimit = Integer.parseInt(systemMapper.selectByPrimaryKey(10).getKeyValue());
        PageHelper.startPage(1, hotListLimit);
        MallGoodsExample hotGoodsExample = new MallGoodsExample();
        MallGoodsExample.Criteria hotGoodsExampleCriteria = hotGoodsExample.createCriteria();
        hotGoodsExampleCriteria.andDeletedEqualTo(false);
        hotGoodsExampleCriteria.andIsHotEqualTo(true);
//        hotGoodsExample.setOrderByClause("sort_order"+" "+"desc");
        List<MallGoods> hotGoodsList = goodsMapper.selectByExample(hotGoodsExample);
        wxHomeIndexVO.setHotGoodsList(hotGoodsList);


        //part8 topicList
        int topicListLimit = Integer.parseInt(systemMapper.selectByPrimaryKey(18).getKeyValue());
        PageHelper.startPage(1, topicListLimit);
        MallTopicExample topicExample = new MallTopicExample();
        MallTopicExample.Criteria topicExampleCriteria = topicExample.createCriteria();
        topicExampleCriteria.andDeletedEqualTo(false);
        List<MallTopic> topicList = topicMapper.selectByExample(topicExample);
        wxHomeIndexVO.setTopicList(topicList);

        //part9 floorGoodsList
        List<WxHomeIndexFloorGoodsListBO> floorGoodsListBO = new ArrayList<>();
//         原始方案 逻辑有问题
//        int categoryGoodsLimit = Integer.parseInt(systemMapper.selectByPrimaryKey(12).getKeyValue());
//        for (MallCategory category : categoryList) {
//            PageHelper.startPage(1, categoryGoodsLimit);
//            MallGoodsExample categoryGoodsExample = new MallGoodsExample();
//            MallGoodsExample.Criteria categoryGoodsExampleCriteria = categoryGoodsExample.createCriteria();
//            categoryGoodsExampleCriteria.andDeletedEqualTo(false);
//            categoryGoodsExampleCriteria.andCategoryIdEqualTo(category.getId());
//            categoryGoodsExample.setOrderByClause("sort_order"+" "+"desc");
//            List<MallGoods> categoryGoodslist = goodsMapper.selectByExample(categoryGoodsExample);
//            floorGoodsList.add(new WxHomeIndexFloorGoodsListBO(category.getName(),categoryGoodslist,category.getId()));
//
//        }
//        wxHomeIndexVO.setFloorGoodsList(floorGoodsList);

        for (MallCategory category : categoryList) {
            MallCategoryExample floorGoodsCategoryExample = new MallCategoryExample();
            floorGoodsCategoryExample.createCriteria().andDeletedEqualTo(false).andPidEqualTo(category.getId());
//            floorGoodsCategoryExample.setOrderByClause("sort_order"+" "+"desc");

            List<MallCategory> mallCategories = categoryMapper.selectByExample(floorGoodsCategoryExample);
            List<Integer> integerList = mallCategories.stream().map(s -> s.getId()).collect(Collectors.toList());
            int floorGoodsLimit = Integer.parseInt(systemMapper.selectByPrimaryKey(12).getKeyValue());
            PageHelper.startPage(1, floorGoodsLimit);
//            List<MallGoods> floorGoodsList=goodsMapper.selectFloorGoods(categoryL2);
            newGoodsListExample.clear();
//            newGoodsListExample.setOrderByClause("sort_order"+" "+"desc");
            newGoodsListExample.createCriteria().andDeletedEqualTo(false).andCategoryIdIn(integerList).andIsOnSaleEqualTo(true);
            List<MallGoods> floorGoodsList = goodsMapper.selectByExample(newGoodsListExample);
            floorGoodsListBO.add(new WxHomeIndexFloorGoodsListBO(category.getName(), floorGoodsList, category.getId()));
        }
        wxHomeIndexVO.setFloorGoodsList(floorGoodsListBO);

        return wxHomeIndexVO;
    }
}
